using System.Linq;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace Empanada
{
    interface IEmpanadaFile
    {
        string ServerPath { get; }
        bool CanGetLatest { get; }
        bool CanAdd { get; }
        bool CanCheckOut { get; }
        bool CanCheckInOrUndo { get; }
    }

    class UnmappedFile : IEmpanadaFile
    {
        public static readonly IEmpanadaFile Instance = new UnmappedFile();

        private UnmappedFile()
        { }

        public string ServerPath
        {
            get { return string.Empty; }
        }

        public bool CanGetLatest
        {
            get { return false; }
        }

        public bool CanAdd
        {
            get { return false; }
        }

        public bool CanCheckOut
        {
            get { return false; }
        }

        public bool CanCheckInOrUndo
        {
            get { return false; }
        }
    }

    class WorkspaceMappedFile : IEmpanadaFile
    {
        public static readonly IEmpanadaFile Instance = new WorkspaceMappedFile();

        private WorkspaceMappedFile()
        { }

        public string ServerPath
        {
            get { return string.Empty; }
        }

        public bool CanGetLatest
        {
            get { return false; }
        }

        public bool CanAdd
        {
            get { return true; }
        }

        public bool CanCheckOut
        {
            get { return false; }
        }

        public bool CanCheckInOrUndo
        {
            get { return false; }
        }
    }

    class VersionControlledFile : IEmpanadaFile
    {
        private readonly string _serverPath;
        private readonly Microsoft.TeamFoundation.VersionControl.Client.PendingChange[] _pendingChanges;

        public VersionControlledFile(string localPath, Workspace workspace)
        {
            _serverPath = workspace.GetServerItemForLocalItem(localPath);
            _pendingChanges = workspace.GetPendingChanges(localPath);
        }

        public string ServerPath
        {
            get { return _serverPath; }
        }

        public bool CanGetLatest
        {
            get { return !_pendingChanges.Any(c => (c.ChangeType & ChangeType.Add) == ChangeType.Add); }
        }

        public bool CanAdd
        {
            get { return false; }
        }

        public bool CanCheckOut
        {
            get { return !_pendingChanges.Any(c => (c.ChangeType & ChangeType.Edit) == ChangeType.Edit); }
        }

        public bool CanCheckInOrUndo
        {
            get { return _pendingChanges.Any(); }
        }
    }
}
